﻿
using Light.Framework.Component;

using Light.Weixin.Model;
using Light.Weixin.Contract;
using NHibernate.Criterion;
using Light.Framework.Model;
using System.Collections.Generic;
using Light.Framework;

namespace Light.Weixin.Component
{
    public class WxMsgTplComponent : BaseComponent<WxMsgTpl>, IWxMsgTplComponent
    {
        public IList<WxMsgTpl> GetMsgTpls(string originalId)
        {
            using (new GuestUser())
            {
                WxMsgTplComponent comp = new WxMsgTplComponent();
                SearchArgs args = new SearchArgs();
                args.Add(SearchLogic.And, "WxApp.OriginalID", originalId);

                return comp.GetAll(args);
            }
        }

        public WxMsgTpl GetMsgTpl(WxApp app, string key)
        {
            using (new GuestUser())
            {
                SearchArgs args = new SearchArgs();
                args.Add(SearchLogic.And, "WxApp", app);
                args.Add(SearchLogic.And, "Key", key);
                return Get(args);
            }
        }

        protected override ICriterion CreateCriterion(WxMsgTpl model)
        {
            return Expression.And(Expression.Eq("Key", model.Key), Expression.Eq("WxApp", model.WxApp));
        }

        protected override System.Func<WxMsgTpl, bool> CreateFunc(WxMsgTpl model)
        {
            return w => w.Key == model.Key && w.WxApp == model.WxApp;
        }

        protected override string GetExistMessage(WxMsgTpl model)
        {
            return model.WxApp.Name + " 下已存在 Key为 " + model.Key + " 的模板";
        }
    }
}
